var mongoose = require('mongoose')
    , Schema = mongoose.Schema
    , Promise = require('bluebird')

module.exports = function (model) {

    var Point = Schema({
        time: {type: Date}
        , open: {type: Number}
        , close: {type: Number}
        , high: {type: Number}
        , low: {type: Number}
        , volume: {type: Number}
        , date: {type: Date}
        , num: {type: String} // 编号
        , name: {type: String}
        , type: {type: Number}  // 0 期货，1 股票
    })

    Point.index({type: 1, num: 1})

    Point.statics.Qsave = function (options) {
        var thisModel = this
        options = options || {}
        return new Promise(function (resolve, reject, notify) {
            thisModel(options).save(function (err, doc) {
                if (err) {
                    reject(err)
                } else {
                    resolve(doc)
                }
            })
        })
    }

    Point.statics.Qadd = function (type, num, name, open, close, high, low, volume, date) {
        var doc = {
            type: type,
            num: num,
            name: name,
            open: open,
            close: close,
            high: high,
            low: low,
            volume: volume,
            date: date,
            time: new Date()
        }
        return this.Qsave(doc)
    }

    Point.statics.Qfind = function (query, sort, skip, limit) {
        query = query || {}
        var thisModel = this
        return new Promise(function (resolve, reject, notify) {
            thisModel.find(query).sort(sort).skip(skip).limit(limit).exec(function (err, docs) {
                if (err) {
                    reject(err)
                } else {
                    resolve(docs)
                }
            })
        })
    }

    Point.statics.QfindByIds = function (ids) {
        return this.Qfind({_id: {$in: ids}})
    }

    Point.statics.QfindOne = function (query) {
        query = query || {}
        var thisModel = this
        return new Promise(function (resolve, reject, notify) {
            thisModel.findOne(query, function (err, doc) {
                if (err) {
                    reject(err)
                } else {
                    resolve(doc)
                }
            })
        })
    }

    Point.statics.Qcount = function (query) {
        query = query || {}
        var thisModel = this
        return new Promise(function (resolve, reject, notify) {
            thisModel.count(query).exec(function (err, count) {
                if (err) {
                    reject(err)
                } else {
                    resolve(count)
                }
            })
        })
    }


    Point.statics.QfindOneAndUpdate = function (query, update, options) {
        var thisModel = this
        return new Promise(function (resolve, reject, notify) {
            thisModel.findOneAndUpdate(query, update, options, function (err, doc) {
                if (err) {
                    reject(err)
                } else {
                    resolve(doc)
                }
            })
        })
    }


    Point.statics.Qupdate = function (query, doc, options) {
        var thisModel = this
        return new Promise(function (resolve, reject, notify) {
            thisModel.update(query, doc, options, function (err, numberAffected, raw) {
                if (err) {
                    reject(err)
                } else {
                    resolve([numberAffected, raw])
                }
            })
        })
    }

    Point.statics.Qremove = function (options) {
        var thisModel = this
        return new Promise(function (resolve, reject, notify) {
            thisModel.remove(options, function (err, doc) {
                if (err) {
                    reject(err)
                } else {
                    resolve(doc.result)
                }
            })
        })
    }

    Point.statics.QfindOneAndRemove = function (query, options) {
        var thisModel = this
        return new Promise(function (resolve, reject, notify) {
            thisModel.findOneAndRemove(query, options, function (err, doc, result) {
                if (err) {
                    reject(err)
                } else {
                    resolve(doc)
                }
            })
        })
    }

    return Point
}